package com.hpjpw.idoapp.controller;

import com.hpjpw.ido.domain.Message;
import com.hpjpw.ido.domain.MessageReceive;
import com.hpjpw.ido.service.IMessageReceiveService;
import com.hpjpw.ido.service.IMessageService;
import com.hpjpw.idoapp.domain.ReceiveMessageListVo;
import com.hpjpw.idoapp.domain.SendSystemMessageVo;
import com.hpjpw.idoapp.service.ReceiveMessageListService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsProject: ruoyi
 * @BelongsPackage: com.hpjpw.idoapp.controller
 * @Author: hpj
 * @CreateTime: 2021-01-06 14:46
 * @Description: 系统消息controller
 */
@Api("系统消息模块")
@RestController()
@RequestMapping("/ido/app/message")
public class IDoSystemMessageController extends BaseController {

    @Autowired
    IMessageReceiveService messageReceiveService;

    @Autowired
    IMessageService messageService;

    @Autowired
    ReceiveMessageListService receiveMessageListService;

    @ApiOperation("获取消息列表，可带分页参数：pageNum=1&pageSize=10")
    @GetMapping("/list")
    public TableDataInfo list() {
        startPage();
        List<ReceiveMessageListVo> receiveMessageListVos = receiveMessageListService.selectMessageReceiveByReceiverId(SecurityUtils.getUserId());
        return getDataTable(receiveMessageListVos);
    }

    @ApiOperation("获取单个消息")
    @GetMapping("/read/{id}")
    public AjaxResult read(@PathVariable Long id) {
        MessageReceive messageReceive_q = new MessageReceive();
        messageReceive_q.setReceiverId(SecurityUtils.getUserId());
        messageReceive_q.setMessageId(id);
        MessageReceive messageReceive = messageReceiveService.selectMessageReceiveByRIdAndMId(messageReceive_q);
        if (StringUtils.isNotNull(messageReceive)) {
            Message message = messageService.selectMessageById(messageReceive.getMessageId());
            messageReceive.setState(1L);
            messageReceiveService.updateMessageReceive(messageReceive);
            return AjaxResult.success(message);
        }
        return AjaxResult.error("您无阅读本消息的权限");
    }

    @ApiOperation("一键所有已阅")
    @PostMapping("/readAll")
    public AjaxResult readAll() {
        MessageReceive messageReceive = new MessageReceive();
        messageReceive.setReceiverId(SecurityUtils.getUserId());
        messageReceive.setState(1L);
        messageReceiveService.updateMessageReceiveByReceiverId(messageReceive);
        return AjaxResult.success("执行成功");
    }

    @ApiOperation("发送系统消息")
    @PostMapping("/send/{id}")
    @PreAuthorize("@ss.hasPermi('ido:message_receive:add')")
    @Log(title = "发送系统消息", businessType = BusinessType.INSERT)
    public AjaxResult send(@PathVariable Long id, @RequestBody SendSystemMessageVo sendSystemMessageVo) {
        List<Long> userIds = receiveMessageListService.getUserIdsFromSendSysMsgVo(sendSystemMessageVo);
        if (StringUtils.isNull(userIds) || userIds.isEmpty())
            return AjaxResult.error("发送错误: 没有找到需要发送的人");
        int num = messageReceiveService.insertMessageReceiveByUserIds(userIds, id);
        return AjaxResult.success("成功将消息发送给" + num + "人");
    }

    /**
     * 删除消息阅读状态
     */
    @PreAuthorize("@ss.hasPermi('ido:message_receive:remove')")
    @Log(title = "撤回系统消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{messageId}")
    @ApiOperation("撤回系统消息")
    public AjaxResult remove(@PathVariable Long messageId)
    {
        return toAjax(receiveMessageListService.deleteMessageReceiveByMessageId(messageId));
    }
}
